package com.yyg.usecase.springbootwebdemo.web;

import com.yyg.usecase.springbootwebdemo.oom_cpu_case.CpuOomSimulationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Controller
public class CpuOomSimulationControllerTest {

    @Autowired
    private CpuOomSimulationService cpuOomSimulationService;

    //cpu100
    //http://127.0.0.1:8888/cpu100/10
    @RequestMapping(value = "/cpu100/{duration}", method = RequestMethod.GET)
    @ResponseBody
    public String cpu100(@PathVariable("duration") String duration) {
        Integer durationInt = Integer.valueOf(duration);
        cpuOomSimulationService.Cpu100Simulation(durationInt);
        return String.format("cpu100 simulation started, duration: %s seconds", duration);
    }

    //内存100
    //http://127.0.0.1:8888/mem100?taskNum=10&taskMem=100&taskDuration=10&taskDelay=100
    @RequestMapping(value = "/mem100", method = RequestMethod.GET)
    @ResponseBody
    public String mem100(@RequestParam("taskNum") String taskNum, @RequestParam("taskMem")String taskMem,
                         @RequestParam("taskDuration")String taskDuration, @RequestParam("taskDelay")String taskDelay) {
        cpuOomSimulationService.MemoryOomSimulation(Integer.parseInt(taskNum), Integer.parseInt(taskMem),
                Integer.parseInt(taskDuration), Optional.ofNullable(taskDelay).map(Integer::valueOf).orElse(null));
        return "mem100 simulation started";
    }

    @RequestMapping(value = "/gc", method = RequestMethod.GET)
    @ResponseBody
    public String systemGc() {
        cpuOomSimulationService.systemGc();
        return "System GC invoked";
    }

}
